package com.ape.mapsa.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ape.mapsa.dao.IUserDAO;
import com.ape.mapsa.model.Role;
import com.ape.mapsa.model.Session;
import com.ape.mapsa.model.User;
import com.ape.mapsa.model.UserRole;

@Service
public class UserService implements IUserService, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1231284683063603555L;
	@Autowired
	IUserDAO userDAO;

	/**
	 * Add User
	 * 
	 * @param User
	 *            user
	 */
	@Transactional(readOnly = false)
	@Override
	public void addUser(User user) {
		getUserDAO().addUser(user);
	}

	/**
	 * Delete User
	 * 
	 * @param User
	 *            user
	 */
	@Transactional(readOnly = false)
	@Override
	public void deleteUser(User user) {
		getUserDAO().deleteUser(user);
	}

	/**
	 * Update User
	 * 
	 * @param User
	 *            user
	 */
	@Transactional(readOnly = false)
	@Override
	public void updateUser(User user) {
		getUserDAO().updateUser(user);
	}

	/**
	 * Get User
	 * 
	 * @param int User Id
	 */
	@Override
	public User getUserByEmail(String email) {
		return getUserDAO().getUserByEmail(email);
	}

	/**
	 * Get User List
	 * 
	 */
	@Override
	public List<User> getUsers() {
		List<User> users = getUserDAO().getUsers();
		for (User user : users) {
			if (user.getEmail().equals("manukyanv07@gmail.com")) {
				users.remove(user);
				break;
			}
		}
		return users;
	}

	public IUserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Override
	public User login(User user) {
		User emailUser = getUserDAO().getUserByEmail(user.getEmail());
		if (emailUser != null && emailUser.getPassword().equals(user.getPassword())) {
			return emailUser;
		} else {
			return null;
		}
	}

	@Override
	public List<Role> getAllRoles() {
		List<Role> roles = getUserDAO().getAllRoles();
		List<Role> mapsaroles = new ArrayList<Role>();
		for (Role role : roles) {
			if (!role.getRoleName().equals("admin") && !role.getRoleName().equals("manager") && !role.getRoleName().equals("manager-gui")
					&& !role.getRoleName().equals("manager-script")) {
				mapsaroles.add(role);
			}

		}
		return mapsaroles;
	}

	@Override
	public List<UserRole> getAllUserRoles(User user) {
		List<UserRole> roles = getUserDAO().getAllUserRoles(user);
		return roles;
	}

	@Transactional(readOnly = false)
	@Override
	public void removeUserRoles(User selectedUser) {
		getUserDAO().removeUserRoles(selectedUser);
	}

	@Override
	public User getById(Long id) {
		return getUserDAO().getUserById(id);
	}

	@Transactional(readOnly = false)
	@Override
	public void addSession(Session session) {
		getUserDAO().addSession(session);
	}

	@Override
	public Session getSessionById(String id) {
		return getUserDAO().getSessionById(id);
	}

	@Override
	public void updateSession(Session session) {
		getUserDAO().updateSession(session);
	}

}
